//
//  StatusListViewModel.swift
//  Swiftsweibo
//
//  Created by 张鹏飞 on 16/1/11.
//  Copyright © 2016年 张鹏飞. All rights reserved.
//

import Foundation

class StatusListViewModel {
/// 微博数据数组
    lazy var statusList = [StatusViewModel]()
    
/// 加载网络数据
    func loadStatus(finished: (isSuccessed: Bool) -> ()) {
    
        NetWorkTools.sharedTools.loadStatus { (result, error) -> () in
            if error != nil {
                
                print("出错了")
                
                finished(isSuccessed: false)
                
                return
            }
            
            //判断数据的格式是否正确
            guard let array = result?["statuses"] as? [[String: AnyObject]] else {
                print("数据格式错误")
                
                finished(isSuccessed: false)
                return
            }
            //遍历字典数组字典转模型
            
            //1.可变数组
            var dataList = [StatusViewModel]()
            
            //2.遍历数组
            for dict in array {
                
                //拼接模型数组
                dataList.append(StatusViewModel(status: Status(dict: dict)))
            }
            
            //3.拼接数据
            self.statusList = dataList + self.statusList
            
//            print(self.statusList)
            
            //4.完成回调
            finished(isSuccessed: true)
           
        }
    }
}

